Graphical and Latent Variable Modeling


Michael Clark
Statistician Lead

Starting Out

Document

m-clark.github.io/docs/sem

Outline

Preliminaries

Graphical Modeling

Latent Variables

SEM

Overview of others

Primary components

Scripts/Docs

Console

Viewer

Other





Scripts/Docs

For this workshop, use traditional R script or notebook

Script is where your code goes

Ctrl+Enter will run current line or selected lines

Console

Where the code is run

Typically your output will be viewed here

Viewer

Visualizations

Intro2R

Basics

Objects

  • Functions

Classes

Installing packages

Code

Using functions to create output

Print the object to view it

myobj = function(arg1=input1, argA=inputA)  
myobj

Code

  • The following creates the object nums

  • sum sums the elements of nums

  • The result is assigned to the object numsSum

  • Object is printed to view it

nums = c(1,2,3,4)      # c is a function that combines its elements
numsSum = sum(nums)     
numsSum
[1] 10

Example lavaan code

library(lavaan)                     # loads the library

myModel = "
 # latent variables aka measurment models
 Factor1 =~ x1 + x2 + x3            
 Factor2 =~ y1 + y2 + y3

 # regresssions aka structural model
 Factor2 ~ Factor1 + z1 + z3
"

mySEM = sem(myModel, otherinputs)   # run the model
summary(mySem)                      # display results

Save your work

Always save your scripts

If desired, can save your work as RData file

save(obj1, obj2, file='filelocation/myRstuff.RData')

save.image('filelocation/myRstuff.RData')     # saves everything

Graphical Models

Overview

Directed Graphs

  • Path Analysis

  • Mediation